package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsTopic;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * projectName: shop-manage
 *
 * @author: 元
 * time: 2021/9/16 0016 21:40
 * description:商场管理:专题管理接口实现
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    private DtsTopicMapper dtsTopicMapper;

    @Override
    public IPage<DtsTopic> selectByPage(Integer page, Integer limit, String title, String subtitle, String sort, String order) {
        Page<DtsTopic> topicPage = new Page<>(page,limit);
        //查询数据
        QueryWrapper queryWrapper = new QueryWrapper();
        if (title !=null){
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(subtitle)){
            queryWrapper.like("subtitle",subtitle);
        }
        if ("asc".equalsIgnoreCase(order)){
            queryWrapper.orderByAsc(sort);
        }else{
            queryWrapper.orderByAsc(sort);
        }

        queryWrapper.eq("deleted", 0);

        IPage<DtsTopic> resultPage = dtsTopicMapper.selectPage(topicPage,queryWrapper);

        return resultPage;
    }


    @Override
    public int updateTopic(DtsTopic dtsTopic) {
        dtsTopic.setUpdateTime(new Date());
        int count = dtsTopicMapper.updateById(dtsTopic);
        return count;
    }

    @Override
    public int removeById(Integer id) {
        int count = dtsTopicMapper.deleteById(id);
        return count;
    }

    @Override
    public DtsTopic addTopic(DtsTopic dtsTopic) {
        dtsTopic.setAddTime(new Date());
        int count = dtsTopicMapper.insert(dtsTopic);
        return dtsTopic;
    }
}
